﻿using System;
using System.Collections.Generic;
using System.Threading;
using TYBase;

namespace TYRTUBase {
    public class XMode_Scaner_Para {
        public double DataCount { get; set; }
        public double ErrorCount { get; set; }
        public double StartSeqp { get; set; }
    }

    public class Device_Scaner : DeviceBase, IDisposable {
        internal byte[] CmdOn = ("LON" + Environment.NewLine).AsBytes();
        internal byte[] CmdOff = ("LOFF" + Environment.NewLine).AsBytes();

        public Device_Scaner(string equipId, string equipName, bool isSysRtu = false, bool isTcp = false) :
            base(EnumEquipType.Scaner,
                equipId, equipName, isSysRtu, isTcp) {
        }

        private int BarCodeCount = 1;

        public void SetBarCodeCount(int codeCount) {
            BarCodeCount = codeCount;
        }


        public virtual bool ReadData(out string barCode) {
            if (IsXModerMode) {
                Random r = new Random();
                int errorCount = 0;
                List<string> listBarCode = new List<string>();
                for (int i = 0; i < xModePara.DataCount; i++) {
                    if (r.Next(1, 10) % 4 == 0 && errorCount < xModePara.ErrorCount) {
                        listBarCode.Add("ERROR");
                        errorCount++;
                    }
                    else {
                        listBarCode.Add($"Test_{xModePara.StartSeqp++}");
                    }
                }
                barCode = string.Join(",", listBarCode);
                return true;
            }
            barCode = string.Empty;
            if (!CheckComOK())
                return false;

            rtu.ClearInBuff();
            rtu.Write(CmdOn);

            //开始等待
            DateTime dtStart = DateTime.Now;
            byte[] dataReadAll = null;
            bool ret = false;
            while (true) {
                Thread.Sleep(10);
                if (IsRun == true && DateTime.Now - dtStart < TimeSpan.FromMilliseconds(ReadOutTime)) {
                    rtu.Read(out byte[] dataTmp);
                    if (dataTmp != null && dataTmp.Length > 0) {
                        dataReadAll = dataReadAll.Append(dataTmp);
                        var listbytes = dataReadAll.Split(NewLine);
                        if (listbytes != null && listbytes.Count > 0) {
                            var lastData = listbytes[listbytes.Count - 1];
                            var index = lastData.Indexof(NewLine);
                            if (index > 0) {
                                byte[] dataget = new byte[index];
                                Array.Copy(lastData, dataget, index);
                                barCode = dataget.AsString();
                                ret = true;
                                break;
                            }
                        }
                    }
                }
                else {
                    ret = false;
                    break;
                }
            }
            rtu.Write(CmdOff);
            return ret;
        }

        public bool SendLON() {
            if (IsXModerMode) {
                return true;
            }
            if (rtu == null)
                return false;
            if (!rtu.IsConnected)
                return false;
            return rtu.Write(CmdOn) == CmdOn.Length;
        }

        public bool SendLONF() {
            if (IsXModerMode) {
                return true;
            }
            if (rtu == null)
                return false;
            if (!rtu.IsConnected)
                return false;
            return rtu.Write(CmdOff) == CmdOff.Length;
        }

        private XMode_Scaner_Para xModePara = new XMode_Scaner_Para();

        public void SetXModePara(XMode_Scaner_Para xMode_Para) {
            xModePara.DataCount = xMode_Para.DataCount;
            xModePara.ErrorCount = xMode_Para.ErrorCount;
            xModePara.StartSeqp = xMode_Para.StartSeqp;
        }

        public void Init(int com) {
            para.Com = com;
            Init();
        }
    }
}